//
//  UDACVerifierAppDelegate.m
//  UDACVerifier
//
//  Created by Jeremy on 6/10/11.
//  Copyright 2011 DRC. All rights reserved.
//

#import "UDACVerifierAppDelegate.h"

@implementation UDACVerifierAppDelegate

@synthesize window, table, fullFileText;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
	lineList = [NSMutableArray arrayWithCapacity:99999];
	[self openNewFile:nil];
}


#pragma mark -


- (void) parseFile
{
	NSLog(@"WE ARE PARSING!");
	if (fullFileText)
	{
		lineList = [fullFileText componentsSeparatedByString:@"\n"];
		[table performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
		NSLog(@"WE ARE DONE!");
	}
	else
	{
		NSLog(@"/CRY!");
	}
}


- (IBAction) openNewFile:(id)sender
{
	NSOpenPanel* panel = [NSOpenPanel openPanel];
	[panel setCanChooseFiles:YES];
	[panel setCanChooseDirectories:NO];
	[panel setResolvesAliases:YES];
	[panel setAllowsMultipleSelection:NO];
	[panel setAllowedFileTypes:nil];		// all file types
	
	[panel beginSheetModalForWindow:window completionHandler:^(NSInteger result) {
		if (result == NSFileHandlingPanelOKButton)
		{
            [panel orderOut:self]; // close panel before we might present an error
			NSString* ourPath = [[panel URL] path];
			NSError* err = nil;
			if (ourPath)
			{
				self.fullFileText = [NSString stringWithContentsOfFile:ourPath encoding:NSMacOSRomanStringEncoding error:&err];
				if (err)
					NSLog(@"Err: %@, %d", [err localizedDescription], [err code]);
				else 
					NSLog(@"GOTCHA!");
			}
			else 
			{
				self.fullFileText = nil;
				NSLog(@"WAA!");
			}
			
			[self parseFile];
		}
	}];

}


#pragma mark Table Stuff


- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
	return [lineList count];
}


- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
//	return [[lineList objectAtIndex:row] objectForKey:kActualLineText];
	return [lineList objectAtIndex:row];
}


@end
